import axios from 'axios'
import service from './commonApi'
// service 循环遍历输出不同的请求方法
let instance = axios.create({
    baseURL: 'https://yx.tfnhg.com',
    // baseURL: 'https://yhq.freei.cc',
    timeout: 1000,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

// 包裹请求方法的容器
const Http = {}

//请求格式/参数的统一
for(let key in service){
    let api = service[key]; // url,method
    //异步方法
    //async 作用：避免进入回调地狱
    Http[key] = async function(
        params,//请求参数 get: url,put,post,patch(data),delete:url
        isFormData = false, // 标识是否是form-data请求
        config = {} //配置参数
    ) {

        let newParams = {}
        //content-type 是否是form-data的判断
        //form-data的转换
        if(params && isFormData){
            newParams = new FormData()
            for(let i in params){
                newParams.append(i,params[i])
            }
        }else{
            newParams = params
        }
        //不同请求的判断
        let response = {}; //请求的返回值
        if(api.method === 'put' || api.method ==='post' || api.method === 'patch'){
            try {
                response = await instance[api.method](api.url,newParams,config)
            } catch (err) {
                response = err
            }
        }else if(api.method === 'delete' || api.method === 'get'){
            config.params = newParams
            try {
                response = await instance[api.method](api.url,config)
            } catch (err) {
                response = err
            }
        }
        return response; //返回请求的响应值
    }
}
// const loading = this.$loading({
//     lock: true,
//     text: '加载中...',
//     spinner: 'el-icon-loading',
//     background: 'rgba(0, 0, 0, 0.7)'
// });
// 拦截器的添加
// 请求拦截器
instance.interceptors.request.use(config=>{
    //发起请求前做些什么
    return config
},()=>{
    //请求错误
    this.$message.error('请求错误，请稍后重试');
    // Toast('请求错误，请稍后重试')
})
// 响应拦截器
instance.interceptors.response.use(res=>{
    //请求成功
    return res.data
},()=>{
    //请求错误
    this.$message.error('请求错误，请稍后重试');
})

export default Http
